﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Threading;

namespace PersonaLibrary
{
	internal static class ThreadHelper
	{
		public static DispatcherObject CreateUiControl<TUi>()
			where TUi : DispatcherObject
		{
			Func<TUi> t = Activator.CreateInstance<TUi>;
			DispatcherOperation operation = ((App)Application.Current).Dispatcher.BeginInvoke(t, DispatcherPriority.Background, null);
			operation.Wait();

			return operation.Result as TUi;
		}

		public static void SetValueToProperty<TUi>(Action<TUi> action, TUi item)
			where TUi : DispatcherObject
		{
			if(Application.Current != null)
				((App) Application.Current).Dispatcher.BeginInvoke(action,
			                                                   DispatcherPriority.Background,
			                                                   new object[]
			                                                   	{
			                                                   		item
			                                                   	}
				);
			//operation.Wait();
		}

		public static void SetValueToPropertySync<TUi>(Action<TUi> action, TUi item)
			where TUi : DispatcherObject
		{
			if (Application.Current != null)
			{
				((App) Application.Current).Dispatcher.Invoke(action,
				                                              DispatcherPriority.Background,
				                                              new object[]
				                                              	{
				                                              		item
				                                              	}
					);

			}
		}
	}
}
